package com.lucan.tracking.compiler;

import com.google.auto.service.AutoService;
import com.lucan.tracking.annotations.Configuration;
import com.lucan.tracking.enums.Action;
import com.lucan.tracking.enums.Sequence;
import com.squareup.javapoet.*;

import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import java.io.IOException;
import java.util.Set;

import static com.lucan.tracking.utils.Consts.*;

/**
 * @author qihailong
 * @date 2018/7/10 下午2:47
 * @description ConfigurationProcessor
 */

@AutoService(Processor.class)
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class ConfigurationProcessor extends BaseProcessor {

    @Override
    public Class[] getSupportAnnotations() {
        return new Class[]{Configuration.class};
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv) {
        if (set != null && set.size() != 0) {
            Set<? extends Element> elements =
                    roundEnv.getElementsAnnotatedWith(Configuration.class);
            try {
                getLogger().info("Found @Configuration, start...");
                parseConfiguration(elements);
                getLogger().info("Parse @Configuration complete");
            } catch (Exception e) {
                getLogger().error(e);
            }
            return true;
        }
        return false;
    }

    /**
     * @param elements
     */
    private void parseConfiguration(Set<? extends Element> elements) throws IOException {
        if (elements == null || elements.size() == 0) {
            getLogger().error("no @Configuration are found");
            return;
        }
        if (elements.size() > 1) {
            getLogger().error("too many @Configuration are found");
            return;
        }
        for (Element element : elements) {
            Configuration configuration = element.getAnnotation(Configuration.class);
            String url = configuration.api();
            getLogger().info("url: " + url);
            JavaFile.builder(
                    PACKAGE_OF_CONFIG_FILE,
                    TypeSpec.classBuilder(CONFIG_FILE_NAME)
                            .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
//                            .addField(buildStaticConfigField(CONFIG_FIELD_URL, String.class,
//                                    "$S", url))
                            .addMethod(MethodSpec.methodBuilder(CONFIG_METHOD_URL)
                                    .addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC)
                                    .returns(TypeName.get(String.class))
                                    .addCode(CodeBlock.builder().add("return \"" + url + "\";\n").build())
                                    .build())
                            .build()
            ).build().writeTo(getFiler());
        }
    }
}
